package org.centsuse.intention_spring.invention.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

/**
 * 加载配置文件并json化
 */
@Slf4j
public class LoadFileUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Map<String, Object> loadFileToObj(String filepath) {
        try {
            return (Map<String, Object>) objectMapper.readValue(new File(filepath), Map.class);
        } catch (IOException e) {
//            log.error("Error loading JSON file to map: {}", e.getMessage());
            return Collections.emptyMap();
        }
    }

    // 支持泛型类型的方法（可选）
    public static <T> T loadJsonFile(String filepath, Class<T> valueType) {
        try {
            return objectMapper.readValue(new File(filepath), valueType);
        } catch (IOException e) {
            log.error("Error loading JSON file: {}", e.getMessage());
            return null;
        }
    }
}
